package com.jiuli.http;// Copyright 2000-2022 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.

import com.intellij.codeInsight.hint.HintManager;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationGroupManager;
import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.impl.ImaginaryEditor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.ui.MessageType;
import com.intellij.openapi.util.IconLoader;
import com.intellij.ui.components.*;
import com.intellij.ui.table.JBTable;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.Spacer;
import com.intellij.util.ui.FormBuilder;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.ListTableModel;
import com.intellij.util.ui.table.JBListTable;
import com.intellij.util.ui.table.JBTableRowEditor;
import com.intellij.util.ui.table.JBTableRowRenderer;
import com.intellij.util.ui.table.TableModelEditor;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import javax.swing.event.ListDataListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableModel;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Objects;
import java.util.Vector;

/**
 * Supports creating and managing a {@link JPanel} for the Settings Dialog.
 */
public class AppSettingsComponent {

  private final JPanel myMainPanel=new JPanel(new BorderLayout(5,5));
  private final JPanel myMainPanel1=new JPanel();;
  private final JBTextField myUserNameText = new JBTextField();
  private final JBCheckBox myIdeaUserStatus = new JBCheckBox("Do you use IntelliJ IDEA? ");
  private final JButton jButton = new JButton("新增");
  private final JButton jButton1 = new JButton("删除");
  private final JBTable myListTable=new JBTable();
  private boolean isChange=false;
  private String newValue;
  private String oldvalue;
  String [][] oldTable;
  DefaultTableModel  tableModel;
  public AppSettingsComponent() {
    AppSettingsState settings = AppSettingsState.getInstance();

    String[] columnNames = {"环境名称","URL地址"};   //列名
    String [][]tableVales=settings.urlList;
    oldTable=settings.urlList;
      tableModel = new DefaultTableModel(tableVales,columnNames);
    myListTable.setName("url信息维护");
    myListTable.setModel(tableModel);
    myListTable.setRowHeight(35);
    myListTable.getColumnModel().getColumn(0).setPreferredWidth(80);
    myListTable.getColumnModel().getColumn(1).setPreferredWidth(200);
    JBScrollPane scrollPane = new JBScrollPane(
            myListTable,
            ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED
    );

    myMainPanel.add(scrollPane, BorderLayout.CENTER);
    myMainPanel1.setLayout(new GridLayoutManager(3, 2, JBUI.emptyInsets(), -1, -1));


    //jButton1.setIcon(new ImageIcon(getClass().getResource("/icons/sub.png")));
    myMainPanel1.add(jButton, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
    final Spacer spacer1 = new Spacer();
    myMainPanel1.add(spacer1, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null, 0, false));
    final Spacer spacer2 = new Spacer();
    myMainPanel1.add(spacer2, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_VERTICAL, 1, GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
    myMainPanel1.add(jButton1, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));

    myMainPanel.add(myMainPanel1, BorderLayout.EAST);
    myListTable.getModel().addTableModelListener(new TableModelListener() {
      @Override
      public void tableChanged(TableModelEvent e) {
        if(e.getType() == TableModelEvent.UPDATE){
          newValue = myListTable.getValueAt(e.getLastRow(),e.getColumn()).toString();
          if(!newValue.equals(oldvalue)) {
            isChange=true;
          }}
      }
    });
    myListTable.addMouseListener(new MouseAdapter(){

      @Override
      public void mouseClicked(MouseEvent e){
        oldvalue = myListTable.getValueAt(myListTable.getSelectedRow(),myListTable.getSelectedColumn()).toString();
      }
    });
    jButton.addActionListener(e->{
      tableModel.addRow(new Object[]{"",""});
    });

    jButton1.addActionListener(e->{
      int selectedRow = myListTable.getSelectedRow();
      if (selectedRow!=-1){
        tableModel.removeRow(myListTable.getSelectedRow());
        isChange=true;
      }else {
        Notification notification = new Notification("test","错误:","请选择需要删除的行",NotificationType.ERROR);
              Notifications.Bus.notify(notification);

      }

    });

//    myMainPanel = FormBuilder.createFormBuilder()
//        .addLabeledComponent(new JBLabel("Enter user name: "), myUserNameText, 1, false)
////        .addComponent(myIdeaUserStatus, 1)
//            .addComponent(myListTable)
//        .addComponentFillVertically(new JPanel(), 0)
//        .getPanel();
  }

  public JPanel getPanel() {
    return myMainPanel;
  }

  public JComponent getPreferredFocusedComponent() {
    return myListTable;
  }



  public boolean getChangeFlag() {
    return isChange;
  }

  public String[][]  getTable() {

    int rowCount = myListTable.getRowCount();
    String[][] table = new String[rowCount][2];
    String s = myListTable.getValueAt(0, 1).toString();
    for (int i=0;i<rowCount;i++){
      table[i][0]=myListTable.getValueAt(i, 0).toString();
      table[i][1]=String.valueOf(myListTable.getValueAt(i, 1));
    }
    return table;
  }

  public void   setTable(String[][] colums) {
    String[] columnNames = {"环境名称","URL地址"};
    tableModel.setDataVector(colums,columnNames);
  }



}
